///公共请求模块 添加

/**
 * 附件上传
 * @param {*附件} formFile
 */
export const uploadFile = async (formFile) => {
    if (typeof $.showLoading == 'function') {
        $.showLoading('上传中...');
    }
    return new Promise((resolve, reject) => {
        return $.ajax({
            "url": Utils.CurDomain.SysCfgUrl + "/yyattachments",
            "headers": {
                "Authorization": "Bearer " + Utils.GetSNAuthAT(),
                // "Authorization": "Bearer " + `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aSI6Im9vYmN6IiwidW4iOiLluIjot4MiLCJ1dCI6IjMyIiwiYXQiOiIyIiwiY291aSI6Im9vY2NTIiwiY291biI6IuS6keWNl-elnuWGnOmZhuiJr-eMquS4muaciemZkOWFrOWPuCIsInMiOiJIVE1MIiwidGkiOiJTTkpUIiwibmJmIjoxNzAyMjg0NTEyLCJleHAiOjE3MDIyODYzMTIsImlhdCI6MTcwMjI4NDUxMiwiaXNzIjoiKiIsImF1ZCI6IioifQ.EIzxsf30m_KJ1LcOZXip-vaD-zZwQ5TQR-zMOW4HXvs`,
                "Accept": "application/json"
            },
            "type": 'POST',
            "data": formFile,
            "enctype": 'multipart/form-data',
            "contentType": false,
            "processData": false,
            "cache": false,
            "async": false,
            "type": 'POST',
            success: (res, textStatus, xhr) => {
                if (typeof $.showLoading == 'function') {
                    $.hideLoading();
                }
                resolve(res, textStatus, xhr);
            },
            "error": (xhr, textStatus, error) => {
                if (typeof $.showLoading == 'function') {
                    $.hideLoading();
                }
                Utils.AjaxError(xhr, textStatus, error);
                reject(xhr, textStatus, error);
            }
        });
    })
}
/**
   * 根据图片id获取图片地址
   * @param {*图片id} id
   * @returns
   */
export const requestFilePath = async function (id) {
    return _request(`${Utils.CurDomain.SysCfgUrl}/yyattachments/${id}/url`, "GET");
}

//请求
const _request = async function (url, type, data) {
    return $.ajax({
        "url": url,
        "beforeSend": Utils.AjaxBeforeSend,
        "dataType": "json",
        "data": data,
        "type": type,
        "error": Utils.AjaxError
    });
}

/**
   * 根据附件名称长度判断是否需要截取
   * @param {*附件名称} file
   * @returns
   */
export const publicFileDraw = function (file, _vm) {
    let href = window.location.pathname; //获取当前模块路径
    let arr = href.substr(href.indexOf("html")).split(/[/.]/g);

    if (file.length > 11) {
        setTimeout(function () {
            $.hideLoading();
        }, 200);
        let suffix = file.split('.').pop().toLowerCase(); //截取附件后缀
        let name = file.slice(0, 11 - (suffix.length) - 1); //计算截取文件的11位字符
        $.modal({
            autoClose: false,
            title: '附件上传.名称超长提示',
            text: `<p>附件名称【<span style="color:red"> ${file}</span>】长度超过11个字符，是否自动截取附件名称为【<span style="color:red"> ${name + '.' + suffix}</span>】?</p>`,
            buttons: [
                {
                    text: "取消", className: "default", onClick: function () {
                        $.closeModal();
                        $.hideLoading();
                        $.toptip('文件名称长度不能超过11个字符', 'warning');

                        $("#uploaderFiles li").eq($("#uploaderFiles li").length - 1).remove();
                        _vm.uploadCount = --_vm.uploadCount;

                        // 添加3张后隐藏上传按钮
                        if (_vm.uploadCount == 3) {
                            $(".weui-uploader__input-box").hide();
                        } else {
                            $(".weui-uploader__input-box").show();
                        }
                    }
                },
                {
                    text: "确认", onClick: function () {

                    }
                }
            ]
        });
    } else {
        return `${[arr[arr.length - 2]]}_${new Date().Format("yyyyMMdd")}_${file}`; //截取html前面模块名称 附件重命名
    }
}

/**
 * 用截取后的附件重新上传
 * @param {*附件} file
 */
export const sliceFileName = (upFiles) => {
    let suffix = upFiles.name.split('.').pop().toLowerCase(); //截取附件后缀
    let name = upFiles.name.slice(0, 11 - (suffix.length) - 1); //计算截取文件的11位字符
    return `${name + '.' + suffix}`;
}